package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.behavior.dto.LikesBehaviorDto;
import com.heima.behavior.entity.ApLikesBehavior;
import com.heima.behavior.entity.UpdateArticleMessage;
import com.heima.behavior.service.ILikeService;
import com.heima.common.dto.ResponseResult;
import com.heima.common.dto.User;
import com.heima.common.util.AppThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
@Service
public class LikeServiceImpl implements ILikeService {
//   这个是mongodb数据库
    @Autowired
    private MongoTemplate mongoTemplate;
//    这个是消息中加减
    @Autowired
    private KafkaTemplate kafkaTemplate;
    @Value("${topic.like_behavior}")
    private String LikeTopic;






//    给文章喜欢的行动
    @Override
    public ResponseResult saveLike(LikesBehaviorDto dto) {
//        首先先构造一个app文章点赞的对象
        ApLikesBehavior apLikesBehavior=new ApLikesBehavior();
        User user = AppThreadLocalUtil.get();
//        判断是用用户id还是手机id

        if(user!=null){
            apLikesBehavior.setUserId(user.getUserId());
        }else {
            apLikesBehavior.setEquipmentId(dto.getEquipmentId());
        }
        apLikesBehavior.setArticleId(dto.getArticleId());
        apLikesBehavior.setType(dto.getType());
        apLikesBehavior.setOperation(dto.getOperation());
        apLikesBehavior.setCreatedTime(new Date());
//        可以开始判断是否有点赞记录，没有直接添加,有的话判断和数据库的状态是否一致，不一致就该
//可以根据文章id和用户id查找
        Query query=new Query();
        query.addCriteria(Criteria.where("articleId").is(dto.getArticleId()));
        if (user!=null){
            query.addCriteria(Criteria.where("userId").is(dto.getUserId()));

        }else {
            query.addCriteria(Criteria.where("equipmentId").is(dto.getEquipmentId()));
        }

        ApLikesBehavior one = mongoTemplate.findOne(query, ApLikesBehavior.class);
//        再继续判断是否存在
        if(one==null){
            if(dto.getOperation()==0){
                mongoTemplate.save(apLikesBehavior);
                sendMessage(dto);
            }
        }else {
//            判断是否一致状态
            if(dto.getOperation()!=(one.getOperation())){
//                不长一智就该
                one.setOperation(dto.getOperation());
                mongoTemplate.save(one);
                sendMessage(dto);

            }

        }


        return ResponseResult.okResult();
    }
//获得喜欢的列表
    @Override
    public ResponseResult getLikes(LikesBehaviorDto dto) {
        Query query=new Query();
        query.addCriteria(Criteria.where("articleId").is(dto.getArticleId()));
        if(dto.getUserId()!=null){
            query.addCriteria(Criteria.where("userId").is(dto.getUserId()));
        }else {
            query.addCriteria(Criteria.where("equipmentId").is(dto.getEquipmentId()));
        }
        ApLikesBehavior one = mongoTemplate.findOne(query, ApLikesBehavior.class);
        return ResponseResult.okResult(one);
    }

    //    发送点赞消息kafka
    private void sendMessage(LikesBehaviorDto dto){
//        发送消息
        UpdateArticleMessage message=new UpdateArticleMessage();
        message.setType(1);
        message.setArticleId(dto.getArticleId());
        message.setAdd(1);
//        判断是并不是点赞
        if(dto.getOperation()==1){
            message.setAdd(-1);

        }
        String value = JSON.toJSONString(message);
        kafkaTemplate.send(LikeTopic,value);

    }





}
